package com.fub.view.mapcomponents;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;

import com.fub.guidance.Point;
import com.fub.guidance.Vertex;
import com.fub.util.Util;
import com.fub.util.PreferenceDAO;

public class BigLabelComponent {

	private static final int TOOLTIP_HEIGHT = 30;
	private static final int SPACING = 5;
	private static final int BACKGROUND_PADDING = 2;
	private static final int TEXT_SIZE = 13;
	private static final String PROPERTY_TO_SHOW = "Nutzung";
	private Vertex node;
	private Paint paintText;
	private Paint paintTextBold;
	private Paint paintToolTipPath;
	private int color;
	private PreferenceDAO preferenceDAO;

	public BigLabelComponent(Vertex node, Bitmap icon, Bitmap expandIcon,
			PreferenceDAO preferenceDAO, int color) {
		this.preferenceDAO = preferenceDAO;
		this.node = node;
		this.color = color;
		initPaints();
	}

	private void initPaints() {
		// normal text
		this.paintText = new Paint();
		this.paintText.setAntiAlias(true);
		this.paintText.setStyle(Paint.Style.FILL);
		this.paintText.setTextSize(11);
		this.paintText.setColor(Color.WHITE);
		// heading
		this.paintTextBold = new Paint();
		this.paintTextBold.setAntiAlias(true);
		this.paintTextBold.setTypeface(Typeface.DEFAULT_BOLD);
		this.paintTextBold.setStyle(Paint.Style.FILL);
		this.paintTextBold.setTextSize(13);
		this.paintTextBold.setColor(Color.WHITE);
		// background of the tooltip
		this.paintToolTipPath = new Paint(Paint.ANTI_ALIAS_FLAG);
		this.paintToolTipPath.setColor(this.color);
		this.paintToolTipPath.setStrokeWidth(1);

	}

	public void draw(Canvas canvas) {
		String text = getSubText();
		this.node.getProperty("person");
		Point positionOnDisplay = Util.calculateZoomedPosition(
				this.node.getPosition(), this.preferenceDAO);
		int textWidthSubText = Util.getTextWidth(text, this.paintText);
		int textWidthLabel = Util.getTextWidth(this.node.getLabel(),
				this.paintTextBold);
		int textWidth = textWidthLabel;
		if (textWidthSubText > textWidth) {
			textWidth = textWidthSubText;
		}
		int tooltipWidth = textWidth + 2 * SPACING;
		positionOnDisplay.move(-tooltipWidth / 2, -TOOLTIP_HEIGHT / 2);
		Path p = getTooltipBackgroundPath(positionOnDisplay.getX(),
				positionOnDisplay.getY() - TOOLTIP_HEIGHT, tooltipWidth,
				TOOLTIP_HEIGHT);
		canvas.drawPath(p, this.paintToolTipPath);
		canvas.drawText(this.node.getLabel(), positionOnDisplay.getX()
				+ SPACING, positionOnDisplay.getY() + TEXT_SIZE
				- TOOLTIP_HEIGHT, this.paintTextBold);
		canvas.drawText(text, positionOnDisplay.getX() + SPACING,
				positionOnDisplay.getY() + 2 * TEXT_SIZE - TOOLTIP_HEIGHT,
				this.paintText);
	}

	private String getSubText() {
		String text = this.node.getProperty(PROPERTY_TO_SHOW);
		if (text.length() == 0) {
			return "no label";
		}
		return text;
	}

	private Path getTooltipBackgroundPath(int offsetX, int offsetY,
			int tooltipWidth, int tooltipHeight) {
		Path p = new Path();
		p.moveTo(offsetX + BACKGROUND_PADDING, offsetY);
		p.lineTo(offsetX + tooltipWidth - BACKGROUND_PADDING, offsetY);
		p.lineTo(offsetX + tooltipWidth, offsetY + BACKGROUND_PADDING);
		p.lineTo(offsetX + tooltipWidth, offsetY + tooltipHeight
				- BACKGROUND_PADDING);
		p.lineTo(offsetX + tooltipWidth - BACKGROUND_PADDING, offsetY
				+ tooltipHeight);
		p.lineTo(offsetX + tooltipWidth / 2 + 10, offsetY + tooltipHeight);
		p.lineTo(offsetX + tooltipWidth / 2, offsetY + tooltipHeight + 10);
		p.lineTo(offsetX + tooltipWidth / 2 - 10, offsetY + tooltipHeight);
		p.lineTo(offsetX + BACKGROUND_PADDING, offsetY + tooltipHeight);
		p.lineTo(offsetX, offsetY - BACKGROUND_PADDING + tooltipHeight);
		p.lineTo(offsetX, offsetY + BACKGROUND_PADDING);
		p.lineTo(offsetX + BACKGROUND_PADDING, offsetY);
		return p;
	}
}
